package ru.ifmo.ctd.intsys.afanasyeva.neural.digits;

import java.io.File;
import java.io.IOException;

import ru.ifmo.ctd.intsys.afanasyeva.neural.MyNeuralNetwork;

public class Digits {	

	public static void main(String[] args) throws IOException {
		int steps = 100000;
		//int digits = 10;
		
		int width = 3;
		int height = 5;
		
		String path = "data/sample/";
		//String end = ".bmp";
		
		File[] samples = (new File(path)).listFiles();
				
		double[][] inputs = new double[samples.length][];
		double[][] answers = new double[samples.length][1];
				
		for (int i = 0; i < samples.length; i++) {
			inputs[i] = ImageUtils.imageToBits(samples[i].getPath(), width, height);
			answers[i][0] = (double) Integer.parseInt(samples[i].getName().substring(0, 1)) / 10;
		}
		int inSize = inputs[0].length;
//		System.out.println("Image size: " + inSize + " square pixels");
				
		int outSize = answers[0].length;
		
		MyNeuralNetwork network = 
			new MyNeuralNetwork(new int[]{inSize, /*(int)(inSize * 2.5+ 0.5),*/ inSize, outSize}, inSize, 0.35, steps);
		
//		network.train(inputs, answers);
//		network.save("MostSamples.txt");
		network.load("ManySamples.txt");
		
		File[] tests = (new File("data/tests")).listFiles();
		
		for (int i = 0; i < tests.length; i++) {
			double testAnsw = network.getOutput(ImageUtils.imageToBits(tests[i].getPath(), width, height))[0];
			System.out.println("Input: " + tests[i].getName() + " Test answer: " + ImageUtils.toAnswer(testAnsw));
		}
		
//		System.out.println("\nResults on the training set: ");
//		for (int i = 0; i < inputs.length; i++) {
//			for (int j = 0; j < outSize; j++) {
//			double answ = network.getOutput(inputs[i])[0];
//			System.out.print("Sample: " + samples[i].getName() + " " + /*answ + */" answer: " + Utils.toAnswer(answ));
//			}
//			System.out.println();
//		}
		
//		double testAnsw = network.getOutput(Utils.imageToBits("data/tests/1.bmp", width, height))[0];
//		System.out.println("\nTest answer: " + Utils.toAnswer(testAnsw));
		
	}
}
